// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    internal class SqlConditionBuilder : SqlBuilderBase
    {
        public SqlConditionBuilder(
            SqlConditionType conditionType, bool isEmbedded)
            : base(Guid.NewGuid().ToString())
        {
            this.Condition = new ConditionStatement(conditionType, isEmbedded);
        }

        private SqlConditionBuilder(string name) : base(name)
        {

        }

        /// <summary>
        ///     Gets the SQL statement type of the current object.
        /// </summary>
        public override SqlStatementType SqlType => this.Condition.StatementType;

        internal override SqlBuildStatement Statement => this.Condition;

        public ConditionStatement Condition { get; private set; }

        /// <summary>
        ///     Validates the raw SQL string for the specified database generated by the current sqlbuilder.
        /// </summary>
        /// <param name="context">A DataContext object which contains the database connection information.</param>
        /// <param name="throwError">
        ///     True (default ) -- to throw an exception if the raw SQL is not valid in the specified database;
        ///     False -- Not to throw an exception if the raw SQL is not valid in the specified database.
        /// </param>
        /// <returns>
        ///     Returns true if the raw SQL is valid in the specified database; false if the raw SQL is not valid in the specified database.
        /// </returns>
        public override bool Validate(DataContext context, bool throwError = true)
        {
            throw new NotSupportedException();
        }

        public override ISqlBuilder Clone()
        {
            return new SqlConditionBuilder(this.Name)
            {
                Condition = (ConditionStatement)this.Condition.Clone()
            };
        }

        /// <summary>
        ///     Returns the raw SQL string for the corresponding database by the current object.
        /// </summary>
        /// <param name="context">
        ///     A DataContext object which contains the database connection information.
        /// </param>
        /// <returns>Returns the raw SQL string.</returns>
        public override string ToSqlString(DataContext context)
        {
            var sqlText = base.ToSqlString(context);

            if (!this.Condition.IsEmbedded)
            {
                switch (this.Condition.ConditionType)
                {
                    case SqlConditionType.Where:
                    {
                        sqlText = " WHERE " + sqlText;
                    }
                    break;
                    case SqlConditionType.JoinOn:
                    {
                        sqlText = " ON " + sqlText;
                    }
                    break;
                    case SqlConditionType.Having:
                    {
                        sqlText = " HAVING (" + sqlText + ")";
                    }
                    break;
                }
            }

            return sqlText;
        }
    }
}
